package com.sage.ierp.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.sage.commons.AssertUtils;
import com.sage.dao.CompanyDao;
import com.sage.dao.model.Company;
import com.sage.dao.model.ServerISP;
import com.single.commons.StringUtils;
import com.single.exceptions.AssertException;


/**
 * 选择ISP
 * @author Administrator
 *
 */
@Controller
public class ISPChooseController{

	@Autowired
	private CompanyDao companyDao;
	
	
	@RequestMapping("/{pCompanyPnkey}")
	public ModelAndView ispChoose(@PathVariable("pCompanyPnkey") String pCompanyPnkey, HttpServletRequest request) throws IOException, ServletException {
        System.out.println("@@@");
        ModelAndView modelView = new ModelAndView();
        System.out.println("pCompanyPnkey = " + pCompanyPnkey);
        try{
			AssertUtils.assertText(pCompanyPnkey, "10262", "请提供企业编号!");
			Company company = companyDao.findByKey(pCompanyPnkey);
			AssertUtils.assertNull(company, "10268","目前没有存在编号为\"?\"的企业,请提供正确的企业编号!", '?', pCompanyPnkey);
			modelView.addObject("company", company);
			List<ServerISP> sIsps = company.getServer().getServerISPs();
			Map<String, String> pathInfos = new HashMap<String, String>();
			for(ServerISP isp : sIsps){
				pathInfos.put(isp.getIsp().getPnKey(), getAccessPath(company, isp));
			}
			modelView.addObject("pathInfos", pathInfos);
			modelView.setViewName(isIE(request) ? "isp_choose_ie8" : "isp_choose");
		}catch (AssertException e) {
			modelView.addObject("exception", e);
			modelView.setViewName("index");
           // modelView.setViewName("ierp_prompt");
            return modelView;
		}
		return modelView;
	}

    @RequestMapping("/choose")
    public ModelAndView choose(){
        ModelAndView modelView = new ModelAndView();
        modelView.setViewName("mindex");
        return modelView;
    }

    @RequestMapping("/choose2")
    @ResponseBody
    public  Map<String, Object>  choose2(@RequestParam String ip){
        System.out.println("ip = " + ip);
        Map<String, Object> data = new HashMap<String, Object>();
        try{
        Company company = companyDao.findByKey(ip);
        AssertUtils.assertNull(company, "10268","目前没有存在编号为\"?\"的企业,请提供正确的企业编号!", '?', ip);
        List<ServerISP> sIsps = company.getServer().getServerISPs();
        Map<String, String> pathInfos = new HashMap<String, String>();
        for(ServerISP isp : sIsps){
            System.out.println(isp.getIsp().getPnKey()+":"+getAccessPath(company, isp));
            pathInfos.put(isp.getIsp().getPnKey(), getAccessPath(company, isp));
        }
        System.out.println( getAccessPath(company,sIsps.get(0)));
        data.put("message",  getAccessPath(company,sIsps.get(0)));
        }catch (AssertException e) {
            System.out.println("e = " + e);
            data.put("message",   "false");
            data.put("e",e.getMessage());
        }
        return data;
    }
	/**
	 * 判断是否是IE
	 * @param request
	 * @return
	 */
	public boolean isIE(HttpServletRequest request){
		String userAgent = request.getHeader("user-agent");
		return userAgent.indexOf("MSIE") > -1 && userAgent.indexOf("Windows")>-1;
	}
	
	
	/**
	 * 将客户和服务器ISP信息转换成Map
	 * @param company
	 * @param serverIsp
	 * @return
	 */
	public String getAccessPath(Company company, ServerISP serverIsp){
		Map<String, String> map = new HashMap<String, String>();
		map.put("addr", StringUtils.defaultIfEmpty(serverIsp.getDomainName(), serverIsp.getIp()));
		Integer port = serverIsp.getPort();
		map.put("port", (port==null ? "" : ":" + port));
		map.put("erp_name", company.getErpName());
		map.put("isp", serverIsp.getIsp().getPnKey());
		return StringUtils.replace("http://{addr}{port}/{erp_name}?{isp}", map);
	}
}
